package com.antis.olsl.user.login;

import android.text.TextUtils;

import com.antis.olsl.base.net.BaseSubscriber;
import com.antis.olsl.base.net.ExceptionHandle;
import com.antis.olsl.base.utils.LogUtils;
import com.antis.olsl.base.utils.RxUtil;
import com.antis.olsl.library.MyApplication;
import com.antis.olsl.library.utils.RSAEncryptUtil;
import com.antis.olsl.user.net.ApiServer;
import com.antis.olsl.user.net.RetrofitManager;
import com.antis.olsl.user.response.LoginResp;

import java.util.Map;

public class LoginPresenter implements LoginContract.Presenter {
    private LoginContract.View mView;
    private ApiServer mApiServer;

    LoginPresenter() {
        mApiServer = RetrofitManager.getInstance().getServerApi();
    }

    @Override
    public void takeView(LoginContract.View view) {
        mView = view;
    }

    @Override
    public void login(Map<String, Object> map) {
        String encryptString = RSAEncryptUtil.generateEncryptStr(map);
        LogUtils.e("login map= " + map);
        String token = null != MyApplication.getUserInfo() && !TextUtils.isEmpty(MyApplication.getUserInfo().getToken()) ? MyApplication.getUserInfo().getToken() : "123456";

        mApiServer.login(encryptString, token, 2).compose(RxUtil.<LoginResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<LoginResp>bindToLife())
                .subscribe(new BaseSubscriber<LoginResp>() {
                    @Override
                    public void onNext(LoginResp resp) {
                        if (resp.isOk()) {
                            if (null != resp.getContent()) {
                                mView.loginSuccess(resp);
                            } else {
                                mView.loginFailure("请求失败");
                            }
                        } else if (resp.isTimeOut()) {
                            mView.loginFailure("请求超时");
                        } else if (resp.isSlowNetwork()) {
                            mView.loginFailure("网络缓慢，稍后重试");
                        } else if (resp.isUnauthorized()) {
                            mView.loginFailure("接口未授权");
                        } else {
                            mView.loginFailure("请求失败");
                        }
                    }

                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        if (null != e) {
                            if (!TextUtils.isEmpty(e.message)) {
                                mView.loginFailure(e.message);
                            } else {
                                mView.loginFailure("请求失败");
                            }
                        } else {
                            mView.loginFailure("请求失败");
                        }
                    }
                });
    }

    @Override
    public void subscribe() {

    }

    @Override
    public void unSubscribe() {
        LogUtils.e("LoginPresenter", "unSubscribe");
        mView = null;
    }
}
